package com.j180.controller.petOwner;

import com.alibaba.druid.support.json.JSONUtils;
import com.j180.bean.PageBean;
import com.j180.bean.PetOwner;
import com.j180.bean.ResultBean;
import com.j180.service.petOwner.IPetOwnerService;
import com.j180.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/petOwner")
public class PetOwnerController {

    @Autowired
    private IPetOwnerService petOwnerService;

    @GetMapping("/showAllPetOwners/{condition}")
    //http://localhost:8081/pet/petOwner/showAllPetOwners?currentPage=1&pageSize=2
    public ResultBean<PageBean<PetOwner>> showAllPetOwners(@PathVariable("condition") String condition){

        Map<String,Object> map = (Map<String, Object>) JSONUtils.parse(condition);
        ResultBean<PageBean<PetOwner>> resultBean = new ResultBean<>();
        PageBean<PetOwner> pageBean = petOwnerService.getAllPetOwners(map);
        if (pageBean != null){
            resultBean.setCode(1);
            resultBean.setT(pageBean);
            resultBean.setMsg("ok");
        }else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @GetMapping("/showPetOwnerById/{petOwnerId}")
    public ResultBean<PetOwner> showPetOwnerById(@PathVariable("petOwnerId") int id){
        ResultBean<PetOwner> resultBean = new ResultBean<>();
        PetOwner petOwner = petOwnerService.getPetOwnerById(id);
        if (petOwner != null){
            resultBean.setCode(1);
            resultBean.setT(petOwner);
            resultBean.setMsg("ok");
        }else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    // http://localhost:8081/pet/petOwner/addPetOwner
    // @RequestMapping(value = "/addPetOwner",method = RequestMethod.POST)
    @PostMapping("/addPetOwner")
    public ResultBean<PetOwner> addPetOwner(@RequestBody(required = false) PetOwner petOwner,
                                            @RequestParam(value = "photo",required = false) CommonsMultipartFile file){
        ResultBean<PetOwner> resultBean = new ResultBean<>();

        // 调用上传文件的方法，得到文件的新名字
        String newFileName = UploadFileUtil.newFileName(file);
        // if (!"".equals(newFileName) && newFileName != null){

        // 测试前端的时候没选择文件，先把文件名为null去掉，
        if (!"".equals(newFileName)){
            // 设置petOwner的图片名字

            // 测试前端的时候没选择文件，名字选择为前端书写的名字，这里即不用重新设置值
            // petOwner.setPic(newFileName);
            int count = petOwnerService.addPetOwner(petOwner);
            if(count>0){
                //上传文件  选择的文件 文件路径末尾
                // UploadFileUtil.uploadFile(file,"images\\petOwner");
                resultBean.setCode(1);
                resultBean.setT(petOwner);
                resultBean.setMsg("ok");
            }
        } else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @DeleteMapping("/deletePetOwnerById/{petOwnerId}")
    public ResultBean<PetOwner> deletePetOwnerById(@PathVariable("petOwnerId") int id){
        ResultBean<PetOwner> resultBean = new ResultBean<>();
        int count = petOwnerService.deletePetOwnerById(id);
        if(count>0){
            resultBean.setCode(1);
            resultBean.setMsg("ok");
        }
        else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @PutMapping("/updatePetOwner")
    public ResultBean<PetOwner> updatePetOwner(@RequestBody(required = false) PetOwner petOwner,
                                               @RequestParam(value = "photo",required = false) CommonsMultipartFile file){

        ResultBean<PetOwner> resultBean = new ResultBean<>();

        // 调用上传文件的方法，得到文件的新名字
        String newFileName = UploadFileUtil.newFileName(file);
        // if (!"".equals(newFileName) && newFileName != null){
        // 测试前端的时候没选择文件，先把文件名为null去掉，
        if (!"".equals(newFileName)){
            // 设置petOwner的图片名字

            // 测试前端的时候没选择文件，名字选择为前端书写的名字，这里即不用重新设置值
            // petOwner.setPic(newFileName);


            int count = petOwnerService.updatePetOwnerInfo(petOwner);
            if(count>0){
                //上传文件  选择的文件 文件路径末尾
                // UploadFileUtil.uploadFile(file,"images\\petOwner");
                resultBean.setCode(1);
                resultBean.setT(petOwner);
                resultBean.setMsg("ok");
            }
        }else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }


}